package com.finace.money.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.ParamJsonUtil;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.PasswordBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.net.socket.ConnManager;
import com.finace.money.net.socket.PushReceiver;

import java.util.TreeMap;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2017/12/6.
 */

public class ModifyPasswardActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;
    @BindView(R.id.et_password_old)
    EditText etPasswordOld;
    @BindView(R.id.et_password_new)
    EditText etPasswordNew;
    @BindView(R.id.et_password_new_sure)
    EditText etPasswordNewSure;
    @BindView(R.id.button_password)
    Button buttonPassword;

    @Override
    public int setLayout() {
        return R.layout.activity_modify_passward;
    }

    @Override
    public void init() {
        rlLeft.setOnClickListener(this);
        buttonPassword.setOnClickListener(this);
        tvHeadTitle.setText(R.string.passward_mofiy);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PushReceiver.ACTION_CHANGE_PASSWORD);
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver,intentFilter);
        unregisterReceiver(broadcastReceiver);
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            PasswordBean passwordBean = (PasswordBean) intent.getSerializableExtra(PushReceiver.ACTION_CHANGE_PASSWORD);
            if(passwordBean.getRsp_info().getError_id() == 0){
                UIUtil.showToastSafe(getString(R.string.password_change_sucess));
            }else {
                UIUtil.showToastSafe(passwordBean.getRsp_info().getError_message());
            }
        }
    };

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_left:
                onBackPressed();
                break;
            case R.id.button_password:
                String oldPassword = etPasswordOld.getText().toString();
                String newPassword = etPasswordNew.getText().toString();
                String surePassword = etPasswordNewSure.getText().toString();

                if(StringUtil.isNullString(oldPassword)){
                    UIUtil.showToastSafe(getString(R.string.passward_old_input));
                    return;
                }
                if(StringUtil.isNullString(newPassword)){
                    UIUtil.showToastSafe(getString(R.string.passward_new_input));
                    return;
                }
                if(StringUtil.isNullString(surePassword)){
                    UIUtil.showToastSafe(getString(R.string.please_password_sure));
                    return;
                }
                if(!newPassword.equals(surePassword)){
                    UIUtil.showToastSafe(getString(R.string.password_diff_twice));
                    return;
                }
                if(newPassword.equals(oldPassword)){
                    UIUtil.showToastSafe(getString(R.string.password_must_diff));
                    return;
                }

                TreeMap<String, String> twoDetailMap = new TreeMap<>();
//                twoDetailMap.put("symbols", "SR805,rb1805");
                String account = ACache.get(UIUtil.getContext()).getAsString(Constant.account);
                if(StringUtil.isNotNullString(account)){
                    twoDetailMap.put("Account", account);
                    twoDetailMap.put("OldPassword", oldPassword);
                    twoDetailMap.put("NewPassword", newPassword);
                    TreeMap<String, String> mapDetail = new TreeMap<>();
                    mapDetail.put("msgtype","ChangePassword");
                    mapDetail.put("payload", ParamJsonUtil.getjsonData(twoDetailMap).toString());
                    ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapDetail).toString());
                }

                break;

        }
    }
}
